package cn.hncu.booksManagementSystem.book.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;
import cn.hncu.booksManagementSystem.book.vo.BookQueryModel;

public class QueryPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tfdId;

	private JFrame mainFrame;
	//注入ebi
	private BookEbi ebi = BookEbiFactory.getInstanceOfBookEbi();
	private JTextField tfdName;
	private JTextField tfdOut;
	private JTextField tfdIn;
	private JTextField tfdIn2;
	private JTextField tfdOut2;
	/**
	 * Create the panel.
	 */
	public QueryPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
	}

	public void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("查询图书");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(294, 33, 182, 37);
		add(label);
		
		JLabel lblId = new JLabel("ID：");
		lblId.setHorizontalAlignment(SwingConstants.CENTER);
		lblId.setForeground(Color.BLUE);
		lblId.setFont(new Font("微软雅黑", Font.BOLD, 24));
		lblId.setBounds(68, 140, 90, 32);
		add(lblId);
		
		tfdId = new JTextField("");
		tfdId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdId.setColumns(10);
		tfdId.setBounds(157, 140, 204, 32);
		add(tfdId);
		
		JButton btnQuery = new JButton("查询");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//1收集参数
				String id = tfdId.getText();
				String name = tfdName.getText();
				String strInPrice = tfdIn.getText();
				String strInPrice2 = tfdIn2.getText();
				String strOutPrice = tfdOut.getText();
				String strOutPrice2 = tfdOut2.getText();
				double inPrice = 0.0;
				double inPrice2 = 0.0;
				double outPrice = 0.0;
				double outPrice2 = 0.0;
				if (strInPrice!=null&&strInPrice.trim().length()>0) {
					try {
						inPrice = Double.parseDouble(strInPrice.trim());
					} catch (NumberFormatException e1) {
						JOptionPane.showMessageDialog(null, "图书最小进价格式错误，只能是浮点型");
					}
				}
				if (strInPrice2!=null&&strInPrice2.trim().length()>0) {
					try {
						inPrice2 = Double.parseDouble(strInPrice2.trim());
					} catch (NumberFormatException e1) {
						JOptionPane.showMessageDialog(null, "图书最大进价格式错误，只能是浮点型");
					}
				}
				if (strOutPrice!=null&&strOutPrice.trim().length()>0) {
					try {
						outPrice = Double.parseDouble(strOutPrice.trim());
					} catch (NumberFormatException e1) {
						JOptionPane.showMessageDialog(null, "图书最小售价格式错误，只能是浮点型");
					}
				}
				if (strOutPrice2!=null&&strOutPrice2.trim().length()>0) {
					try {
						outPrice2 = Double.parseDouble(strOutPrice2.trim());
					} catch (NumberFormatException e1) {
						JOptionPane.showMessageDialog(null, "图书最大售价格式错误，只能是浮点型");
					}
				}
				
				//2组织参数
				BookQueryModel bqm = new BookQueryModel();
				bqm.setId(id);
				bqm.setName(name);
				bqm.setInPrice(inPrice);
				bqm.setInPrice2(inPrice2);
				bqm.setOutPrice2(outPrice2);
				bqm.setOutPrice(outPrice);
				//3TODO：调用逻辑层
				Collection<BookModel> resCol = ebi.getBooksByCondition(bqm);
				
				//4导向结果界面
				mainFrame.setContentPane(new ListPanel(mainFrame,resCol));
				mainFrame.validate();
			}
		});
		btnQuery.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnQuery.setBounds(168, 412, 136, 45);
		add(btnQuery);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(469, 412, 136, 45);
		add(btnBack);
		
		JLabel label_2 = new JLabel("名称：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setForeground(Color.BLUE);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_2.setBounds(401, 140, 99, 32);
		add(label_2);
		
		tfdName = new JTextField();
		tfdName.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdName.setColumns(10);
		tfdName.setBounds(492, 140, 188, 32);
		add(tfdName);
		
		tfdOut = new JTextField();
		tfdOut.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdOut.setColumns(10);
		tfdOut.setBounds(173, 321, 188, 32);
		add(tfdOut);
		
		JLabel label_1 = new JLabel("最小售价：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setForeground(Color.BLUE);
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_1.setBounds(58, 317, 120, 32);
		add(label_1);
		
		tfdIn = new JTextField();
		tfdIn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdIn.setColumns(10);
		tfdIn.setBounds(173, 234, 188, 32);
		add(tfdIn);
		
		JLabel label_3 = new JLabel("最小进价：");
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setForeground(Color.BLUE);
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_3.setBounds(58, 234, 120, 32);
		add(label_3);
		
		JLabel label_4 = new JLabel("最大进价：");
		label_4.setHorizontalAlignment(SwingConstants.CENTER);
		label_4.setForeground(Color.BLUE);
		label_4.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_4.setBounds(399, 234, 120, 32);
		add(label_4);
		
		tfdIn2 = new JTextField();
		tfdIn2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdIn2.setColumns(10);
		tfdIn2.setBounds(514, 234, 188, 32);
		add(tfdIn2);
		
		JLabel label_5 = new JLabel("最大进价：");
		label_5.setHorizontalAlignment(SwingConstants.CENTER);
		label_5.setForeground(Color.BLUE);
		label_5.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_5.setBounds(399, 321, 120, 32);
		add(label_5);
		
		tfdOut2 = new JTextField();
		tfdOut2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdOut2.setColumns(10);
		tfdOut2.setBounds(514, 321, 188, 32);
		add(tfdOut2);
	}
}
